package com.bbcare.department.plat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtils;
import com.bbcare.department.plat.service.IServiceManagerService;

@Controller
@RequestMapping("/service/servicemanager")
public class ServiceManagerController {
	Logger logger = Logger.getLogger(ServiceManagerController.class);
	@Autowired
	private IServiceManagerService serviceManagerService;

	@ResponseBody
	@RequestMapping("/patientTrendStatisics")
	public ResultModel<List<Map<String,Object>>> getPatientTrendStatisics(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in getPatientTrendStatisics----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

		// 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String authorId = (String) sessionMap.get("authorId");// 租户id
        String deptId = (String) sessionMap.get("rootGroupId");

        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("authorId",authorId);
        paramMap.put("tenantId",tenantId);
        paramMap.put("deptId",deptId);

        paramMap.put("flag",requestParam.get("flag"));
        paramMap.put("type",requestParam.get("type"));
        List<Map<String,Object>> list = serviceManagerService.getServicePatientTrendStatisics(paramMap);
		logger.info("out getPatientTrendStatisics----arg==");
		return new ResultModel<List<Map<String,Object>>>(list);
	}

	@ResponseBody
	@RequestMapping("/patientTotal")
	public ResultModel<Map<String,Object>> getPatientTotal(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in getPatientTrendStatisics----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

		// 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String authorId = (String) sessionMap.get("authorId");// 租户id
        String deptId = (String) sessionMap.get("rootGroupId");

        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("authorId",authorId);
        paramMap.put("tenantId",tenantId);
        paramMap.put("deptId",deptId);

        Map<String,Object> map = serviceManagerService.getPatientTotal(paramMap);
		logger.info("out getPatientTrendStatisics----arg==");
		return new ResultModel<Map<String,Object>>(map);
	}


	@ResponseBody
	@RequestMapping("/useStatistics")
	public ResultModel<List<Map<String,Object>>> getUseStatistics(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in getPatientTrendStatisics----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

		// 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String deptId = (String) sessionMap.get("rootGroupId");

        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("tenantId",tenantId);
        paramMap.put("deptId",deptId);

        List<Map<String,Object>> list = serviceManagerService.getUseStatistics(paramMap);
		logger.info("out getPatientTrendStatisics----arg==");
		return new ResultModel<List<Map<String,Object>>>(list);
	}
}
